/*
 * @: 
 * @version: 1.0
 * @Author: ziwei
 * @Date: 2021-11-28 23:13:34
 * @LastEditors: ziwei
 * @LastEditTime: 2021-11-30 14:47:28
 */
import React from "react";
import ReactDOM from 'react-dom';
import {createStore,combineReducers,applyMiddleware} from 'redux'
import createSagaMiddleware from "redux-saga";
import * as sagaEffects from 'redux-saga';
import { Provider,connect } from "react-redux";
import prefixNamespace from "./prefixNamespace";

export {connect}
function dva(){
  const app={
    _models:[],//存放model的数组
    model,//函数用来往model数组中添加model元素
    router,//定义路由的函数
    _router:null,//存放路由函数的变量
    start//开启渲染
  }
  function model(modelObject){
    const prefixModel = prefixNamespace(modelObject)
    app._model.push(modelObject);
  }
  //把每个model中的effecths
  function getSagas(app) {
    let sagas = [];
    for(let model of app.models){

    }
  }
  return app
}

function getSaga(effects,model) {
  return function *() {
    for(const key in effects){
      const watcherSaga = getWatcher(key,model.effects[key].model);
      yield sagaEffects.fork(watcherSaga);
    }
  }
}

function getWatcher(key,effect,model) {
  return function *() {
    yield sagaEffects.takeEvery(key,function *(action) {
      yield effect(action,{
        ...sagaEffects,put:action=>sagaEffects.put({...action,type:prefixType(action.type,model)})
      })
    })
  }
}
function prefixType(type,model) {
  if(type.indexOf('/'===-1)){
    return `${model.namespace}${NAMESPACE_SEP}${type}`;//counter/asyncAdd
  }else{
    console.log(`Warning:[sagaEffects.put] counter/add should not be predixed with namespace ${model.namespace}`);
    return type
  } 
}
function getReducer(model) {
  let {state:initialState,reducers} = model;
  return (state=initialState,action)=>{
    let reducer = reducers[action.type];
    if(reducer) return reducer(state,action);
    return state
  }
}
export default dva

/* connect => Provider
Provider => store
store => reducer 
获取每个model的reducers对象，把每个对象变成一个函数
然后再通过combineReducers合并成reducer
*/


